home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / xipProcs / colorseg.proc < prev    next >
Text File  |  2009-04-23  |  3KB  |  90 lines

  1.  
  2. // Load scripted support procedures
  3. //#load "xipProcs/printLayer.proc";
  4. #load "xipProcs/thresholdRange.proc";
  5. #load "xipProcs/colorMap.proc";
  6. #load "sys/stdlib.elf";
  7.  
  8. // Dynamically load necessary functions
  9. LoadClasses (filename: "xeng");
  10.  
  11.  
  12. private
  13. PROCEDURE XIPColorSeg (XIPIMAGE input, BOOLEAN debug)
  14.   RETURNS (XIPIMAGE output)
  15. {
  16.   XIPIMAGE    imgSRGB, imgHSV, mask;
  17.   XIPCOLOR    color, bgcolor, maskcolor;
  18.   XIPCOLORMAP cmap;
  19.   INTEGER     i, classid, bgclassid, ncolors;
  20.   XIPTIMER    timer;
  21.  
  22.   if (debug)
  23.     timer.start ();
  24.  
  25.   // Make local copy of input image
  26.   imgSRGB = input.Copy ();
  27.  
  28.   // Make certain input image is in proper format
  29.   imgSRGB = imgSRGB.interleave (scanline: 1
  30.           ).convert (to: (8)
  31.           ).cspace (outspace: "srgb", precise: 1
  32.           // Convert reduced input image to HSV color space
  33.           ).reduce (by: (4, 4), mode: 0
  34.           ).cspace (outspace: "hsv", precise: 1
  35.           );
  36.  
  37.   // Quantize colors of input image
  38.   ncolors = cmap.exec (input: imgHSV, method: "diverse", maxcolors: 256);
  39.  
  40.   if (debug)
  41.     print "Number of colors found:          " + ncolors;
  42.  
  43.   // Reduce to primitive colors
  44.   cmap.reduce ();
  45.  
  46.   // Convert colormap to SRGB color space and sort
  47.   cmap.changespace (photo: XIP_SRGB_COLOR);
  48.   cmap.sort ();
  49.  
  50.   if (debug)
  51.     print "Time to get colors:             " + timer.elapsed (lap: TRUE);
  52.  
  53.   // Apply colormap to full-sized input image
  54.   imgSRGB = cmap.apply (input: imgSRGB);
  55.  
  56.   if (debug)
  57.     print "Time to apply colors to image: " + timer.elapsed (lap: TRUE);
  58.  
  59.   // Get background color
  60.   bgcolor   = cmap.entries[cmap.nentries-1];
  61.   bgclassid = XIPColorClass (color: bgcolor);
  62.  
  63.   // Add background layer to output image
  64.   output = pattern (constant: bgcolor.c, space: "srgb",
  65.                     size: (imgSRGB.imageWidth, imgSRGB.imageHeight)).exec ();
  66.   output.setMember (member: "color", num: 0, value: bgcolor);
  67.  
  68.   if (debug)
  69.     print "Time to add background layer: " + timer.elapsed (lap: TRUE);
  70.  
  71.   // Add color mask to output image for each "visible" color in colormap
  72.   for (i=0; i<cmap.nentries-1; i++) {
  73.     maskcolor = cmap.entries[i];
  74.     classid   = XIPColorClass (color: maskcolor);
  75.  
  76.     mask = XIPThresholdRange (src: imgSRGB, minLevels: maskcolor.c,
  77.                               maxLevels: maskcolor.c, mode: 2);
  78.  
  79.     mask = mask.convert (to: (1)).exec ();
  80.     output = output.addLayer (image: mask, lindex: 0, color: maskcolor);
  81.  
  82.     if (debug)
  83.       print "Time to create & add mask layer " + (i+1) + ": "
  84.               + timer.elapsed (lap: TRUE);
  85.   }
  86.  
  87.   if (debug)
  88.     print "Time to segment image:         " + timer.elapsed ();
  89. }
  90.